package com.ece750.server;

import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.channel.ChannelMessage;
import com.google.appengine.api.channel.ChannelService;
import com.google.appengine.api.channel.ChannelServiceFactory;
import com.google.appengine.api.xmpp.Message;
import com.google.appengine.api.xmpp.MessageBuilder;
import com.google.appengine.api.xmpp.MessageType;
import com.google.appengine.api.xmpp.XMPPService;
import com.google.appengine.api.xmpp.XMPPServiceFactory;

@SuppressWarnings("serial")
public class XmppServer extends HttpServlet {
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
		      throws IOException {
		
		XMPPService xmppService =
			      XMPPServiceFactory.getXMPPService();
		
		Message msg = xmppService.parseMessage(req);
		
		//Testing code
//		Message m = new MessageBuilder()
//			.withRecipientJids(msg.getFromJid())
//			.withMessageType(MessageType.CHAT)
//			.withBody("Got your message")
//			.build();
//		xmppService.sendMessage(m);
		
		//Want to use Channel API to update the client
		ChannelService channelService = ChannelServiceFactory.getChannelService();
		channelService.sendMessage(new ChannelMessage("d4dietri", msg.getBody()));
		
	}
}
